import global from "../../global";
import Global from "../../global";
import Player from "./player";
import Point_ts from "./point";

/**
 * 对下一回合的投资
 */
export enum Invest{
    SSDS,
}

const {ccclass, property} = cc._decorator;

@ccclass
export default class Small extends cc.Component {

    @property(cc.SpriteFrame)
    Sf:cc.SpriteFrame= null;            //小子只有一张图片
    @property(cc.Node)
    Node_tip:cc.Node = null;            //点击提示
    @property(cc.Sprite)
    Sprite:cc.Sprite = null;            //小子图片

    arr_invest:Set<Invest> = new Set(); //投资

    player:Player = null;   //玩家


    onLoad(){
        this.Node_tip.active = false;
        this.Node_tip.setParent(global.mgr.Layer_tip);
    }

    /**
     * 初始化，移动棋子的时候技能移动tip
     */
    init(){
        this.Node_tip.setPosition(this.node.parent.position);
    }

    /**
     * 获得视野
     */
    getViewArr(){
        let arr = Global.getAround(this.node.parent.getComponent(Point_ts).index);
        return arr;
    }
    
    //开启事件监听
    openOn() {
        this.node.targetOff(this);
        this.node.on(cc.Node.EventType.TOUCH_START,this.select,this);
        this.openEffect();
    }

    /**
     * 被选中，周围落点开启出大监听
     */
    select(){
        //关闭小子和大子的事件监听
        this.closeOn();
        this.player.arr_big.forEach(element=>{
            element.closeOn();
        });
        //周围落点开启监听
        let index = this.node.parent.getComponent(Point_ts).index;
        let arr = Global.getAround(index) 
        arr.forEach(element=>{
            Global.mgr.map_point.get(element).onSmallBorn();
        });
        //自己开启监听，取消行动，其实就是全部归位
        this.node.on(cc.Node.EventType.TOUCH_START,this.cancel,this);
    }

    /**
     * 取消
     */
    cancel(){
        this.player.homing();
    }

    /**
     * 取消全部该落点事件监听
     */
    closeOn(){
        this.node.targetOff(this);
        this.closeEffect();
    }
    
    /**
     * 开启点击提示特效
     */
    openEffect(type:number = 0){
        if(global.is_replay){
            return;
        }
        this.Node_tip.getComponent(cc.Sprite).spriteFrame = global.effect.Arr_sf_tip[type];
        this.Node_tip.active = true;
        this.Node_tip.opacity = 255;
        let action = cc.tween().to(1,{opacity:100}).delay(.2).to(1,{opacity:255});
        cc.tween(this.Node_tip).repeatForever(action).start();
    }

    /**
     * 关闭点击提示特效
     */
    closeEffect(){
       this.Node_tip.active = false;
       this.Node_tip.stopAllActions();
    }

    /**
     * 死亡
     */
    killed(){
        this.player.is_surrender = true;
        //阵法销毁
        this.player.clearZF();
        //大子死亡
        this.player.arr_big.forEach(big_ts=>{
            if(big_ts){
                big_ts.die();
            }
        })
        //小子死亡
        this.node.destroy();
        this.Node_tip.destroy();
        //检查是否只有一个一个玩家存活，游戏结束
        let live = 0;
        let winner = 0;
        global.mgr.game_players.forEach(player=>{
            if(!player.is_surrender){
                live++;
                winner = player.id;
            }
        });
        if(live==1){
            global.mgr.gameOver("normal",winner);
        }
    }

    /**
     * 每次轮到自己回合执行
     */
    oneDay(){
        this.arr_invest.forEach(invest=>{
            if(invest==Invest.SSDS){
                this.player.changeAction(4);
                this.arr_invest.delete(invest);
            }
        })
    }

}
